package com.modudf.common;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringUtils
{
  public static String encodeByMd5(String str)
    throws NoSuchAlgorithmException, UnsupportedEncodingException
  {
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    md5.update(str.getBytes("utf-8"));
    byte[] b = md5.digest();
    
    StringBuffer buff = new StringBuffer("");
    for (int offset = 0; offset < b.length; offset++)
    {
      int i = b[offset];
      if (i < 0) {
        i += 256;
      }
      if (i < 16) {
        buff.append("0");
      }
      buff.append(Integer.toHexString(i));
    }
    String newStr = buff.toString();
    return newStr;
  }
}

